package org.example.crop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.crop.domain.Crop;
import org.example.crop.domain.Grow;
import org.example.crop.service.CropService;
import org.example.crop.service.GrowService;
import org.example.crop.util.MinioUtils;
import org.example.crop.util.Result;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("grow")
public class GrowController {
    //农作物生长记录表
    @Autowired
    private GrowService growService;
    @Autowired
    private MinioUtils minioUtils;
    @Autowired
    private
    CropService cropService;
    @RequestMapping("list")
    public Result list(String cropId){
        List<Grow> list=growService.getList(cropId);
        return Result.success(list);
    }
    //添加生长图片
    @RequestMapping("addPic")
    public Result addPic(MultipartFile file) throws Exception {
        String upload = minioUtils.upload(file);
        System.out.println(upload);
        return Result.success(upload);
    }
    @RequestMapping("addGrow")
    public Result addGrow(@RequestBody Grow grow){
        String cropId = grow.getCropId();
        Date date = new Date();
        System.out.println("");
        grow.setCreateDate(date);
        boolean b = growService.save(grow);
        List<Grow> list=growService.getList(cropId);
        if (list.size()>3){
            LambdaQueryWrapper<Crop> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Crop::getId,cropId);
            Crop crop = cropService.getOne(wrapper);
            crop.setStatus(1);
            cropService.updateById(crop);
        }
        return b?Result.success():Result.fail();
    }


}
